package com.open.cloud.uid.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.util.Date;

/**
 * @author leijian
 * @version 1.0
 * @date 2019/3/31 20:20
 **/
public class DateUtil {

    public static final String PATTERN_ISO_DATE = "yyyy-MM-dd";
    public static final String PATTERN_ISO_TIME = "HH:mm:ss";
    public static final String PATTERN_ISO_DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_ISO_FULL = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String PATTERN_SIMPLE_DATE = "yyyyMMdd";
    public static final String PATTERN_SIMPLE_TIME = "HHmmss";
    public static final String PATTERN_SIMPLE_DATETIME = "yyyyMMddHHmmss";
    public static final String PATTERN_SIMPLE_FULL = "yyyyMMddHHmmssSSS";
    protected static final String[] PATTERNS = {PATTERN_ISO_DATE, PATTERN_ISO_TIME, PATTERN_ISO_DATETIME, PATTERN_ISO_FULL, PATTERN_SIMPLE_DATE,
            PATTERN_SIMPLE_TIME, PATTERN_SIMPLE_DATETIME, PATTERN_SIMPLE_FULL};

    public static String formatDate(Date date, String pattern) {
        String locPattern = pattern;
        if (StringUtils.isEmpty(locPattern)) {
            locPattern = PATTERN_ISO_DATE;
        }
        return DateFormatUtils.format(date, locPattern);
    }

    public static Date parseDate(String strDate) {
        Date date = null;
        if (StringUtils.isEmpty(strDate)) {
            return null;
        }
        try {
            date = DateUtils.parseDate(strDate, PATTERNS);
        } catch (ParseException e) {
            

        }
        return date;
    }


}
